//
//  KZCodeSubmitView.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/27.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

class KZCodeSubmitView: UIControl {

    let contentView = UIView()
    let codeTf = UITextField()
    let saveButton = UIButton()
    
    convenience init() {
        
        self.init(frame: .zero)
        
        setupUI()
    }
    
    deinit {
        print("销毁")
    }

}

// MARK: ------ 布局
extension KZCodeSubmitView {
    
    func setupUI() {
        
        self.addSubview(contentView)
        contentView.backgroundColor = UIColor.white
        contentView.layer.cornerRadius = 8
        contentView.layer.masksToBounds = true
        contentView.snp.makeConstraints { (m) in
            m.left.equalTo(20)
            m.right.equalTo(-20)
            m.centerY.equalTo(self)
            m.height.equalTo(150)
        }
        
        codeTf.font = kFont(14)
        codeTf.textAlignment = .center
        codeTf.placeholder = "请输入订单号（默认自动获取）"
        codeTf.backgroundColor = UIColor.init(white: 0.95, alpha: 1)
        contentView.addSubview(codeTf)
        codeTf.snp.makeConstraints { (m) in
            m.left.equalTo(20)
            m.right.equalTo(-20)
            m.top.equalTo(40)
            m.height.equalTo(30)
        }
        
        
        saveButton.addTarget(self, action: #selector(onButtonClick(_:)), for: .touchUpInside)
        saveButton.layer.cornerRadius = 4
        saveButton.layer.masksToBounds = true
        saveButton.backgroundColor = kYellowColor
        saveButton.titleLabel?.font = kFont(15)
        saveButton.setTitle("保存", for: .normal)
        saveButton.setTitleColor(UIColor.white, for: .normal)
        contentView.addSubview(saveButton)
        saveButton.snp.makeConstraints { (m) in
            m.height.equalTo(30)
            m.width.equalTo(contentView.snp.width).multipliedBy(0.5)
            m.bottom.equalTo(-25)
            m.centerX.equalTo(contentView)
        }
    }
}
    
// MARK: ------ 事件
extension KZCodeSubmitView {
    
    func show() {
        
        self.addTarget(self, action: #selector(dismiss), for: .touchUpInside)
        
        // 窗口
        let window = (UIApplication.shared.delegate as! AppDelegate).window!
        
        // 把自身添加到window上
        window.addSubview(self)
        self.backgroundColor = UIColor.black.withAlphaComponent(0)
        
        self.snp.makeConstraints { (m) in
            m.edges.equalTo(window)
        }
        
        
        contentView.transform = CGAffineTransform.init(scaleX: 0.01, y: 0.01)
        
        
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: [], animations: {
            
            self.backgroundColor = UIColor.black.withAlphaComponent(0.4)
            self.contentView.transform = CGAffineTransform.identity
            
        }) { (fin) in
            if fin {
                
            }
        }
    }
    
    // 消失
    @objc func dismiss() {
        
        UIView.animate(withDuration: 0.25, animations: {
            
            self.backgroundColor = UIColor.black.withAlphaComponent(0)
            
            self.contentView.transform = CGAffineTransform.init(scaleX: 0.01, y: 0.01)
            
        }) { (fin) in
            if fin {
                self.removeFromSuperview()
            }
        }
    }
    
  
    
    @objc func onButtonClick(_ button: UIButton) {
        
        dismiss()
    }
}
